Skip to content

Shallowly bail from coerce_unsized more #142941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jun 24, 2025

We do a lot of coercion in HIR typeck. Most of the time we're just coercing a type to itself, but we always try coerce_unsized even if it's not necessary.

Let's avoid doing that by adding a fast path to coerce_unsized; see the comment in that function.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 24, 2025
@compiler-errors
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@bors
Copy link
Collaborator

bors commented Jun 24, 2025

⌛ Trying commit e0b68fe with merge 6a35eeb...

bors added a commit that referenced this pull request Jun 24, 2025
[perf] Shallowly bail from `coerce_unsized` more
@compiler-errors
Copy link
Member Author

TODO: test with a fast path for &str: CoerceUnsized<&str>.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 24, 2025

☀️ Try build successful - checks-actions
Build commit: 6a35eeb (6a35eeb37ae3dfbce8f5159463405fb81cddd28a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6a35eeb): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.7% [-4.5%, -0.1%] 60
Improvements ✅
(secondary)
-6.7% [-21.6%, -0.1%] 58
All ❌✅ (primary) -0.7% [-4.5%, -0.1%] 60

Max RSS (memory usage)

Results (primary -1.2%, secondary -5.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
-1.2% [-1.2%, -1.2%] 2
Improvements ✅
(secondary)
-5.8% [-10.6%, -1.6%] 9
All ❌✅ (primary) -1.2% [-1.2%, -1.2%] 2

Cycles

Results (primary -4.5%, secondary -15.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.5% [-5.5%, -3.2%] 6
Improvements ✅
(secondary)
-15.4% [-25.4%, -6.1%] 27
All ❌✅ (primary) -4.5% [-5.5%, -3.2%] 6

Binary size

Results (secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) - - 0

Bootstrap: 688.989s -> 689.016s (0.00%)
Artifact size: 371.96 MiB -> 372.08 MiB (0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@compiler-errors
Copy link
Member Author

holy fuck lol

@compiler-errors
Copy link
Member Author

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 24, 2025

⌛ Trying commit 3d115ac with merge 1eb1a24

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 24, 2025
[perf] Shallowly bail from `coerce_unsized` more
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@compiler-errors compiler-errors marked this pull request as ready for review June 24, 2025 17:01
@compiler-errors
Copy link
Member Author

Anyways this is ready to review.

r? @lcnr

@rustbot
Copy link
Collaborator

rustbot commented Jun 24, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 24, 2025
@rustbot rustbot assigned lcnr and unassigned petrochenkov Jun 24, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 24, 2025

☀️ Try build successful (CI)
Build commit: 1eb1a24 (1eb1a24f58bb3d49e9be66d408afa9adf2b30a78, parent: 36b21637e93b038453924d3c66821089e71d8baa)

@rust-timer

This comment has been minimized.

@compiler-errors
Copy link
Member Author

Crashes should be fixed by #142976; they're only coincidentally fixed here by the fast path bc we don't end up trying coerce_unsized.

@compiler-errors compiler-errors changed the title [perf] Shallowly bail from coerce_unsized more Shallowly bail from coerce_unsized more Jun 24, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1eb1a24): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.4% [0.3%, 0.4%] 2
Improvements ✅
(primary)
-0.8% [-4.5%, -0.1%] 80
Improvements ✅
(secondary)
-5.5% [-21.6%, -0.0%] 73
All ❌✅ (primary) -0.8% [-4.5%, 0.2%] 81

Max RSS (memory usage)

Results (primary -0.9%, secondary -3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.3% [1.3%, 1.3%] 1
Regressions ❌
(secondary)
3.9% [3.9%, 4.0%] 2
Improvements ✅
(primary)
-3.1% [-3.1%, -3.1%] 1
Improvements ✅
(secondary)
-5.2% [-10.6%, -1.5%] 11
All ❌✅ (primary) -0.9% [-3.1%, 1.3%] 2

Cycles

Results (primary -2.0%, secondary -12.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-4.1%, -0.8%] 26
Improvements ✅
(secondary)
-12.2% [-24.0%, -0.9%] 31
All ❌✅ (primary) -2.0% [-4.1%, -0.8%] 26

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 689.383s -> 687.827s (-0.23%)
Artifact size: 371.94 MiB -> 372.03 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 25, 2025
@@ -515,11 +515,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
///
/// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
#[instrument(skip(self), level = "debug")]
fn coerce_unsized(&self, mut source: Ty<'tcx>, mut target: Ty<'tcx>) -> CoerceResult<'tcx> {
source = self.shallow_resolve(source);
target = self.shallow_resolve(target);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove these, are we guaranteed that they are shallow resolved? if so, please convert them into debug asserts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we shallowly resolve at the top of the coerce entrypoint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls debug assert then

| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
| ty::Str
| ty::Array(_, _)
| ty::Slice(_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array to slice is always Deref an not CoerceUnsized? 🤔

Copy link
Member Author

@compiler-errors compiler-errors Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slices are the RHS for Unsize, not CoerceUnsized. The latter is for pointers not data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah, remember how this works again/looked it up

@lcnr
Copy link
Contributor

lcnr commented Jun 26, 2025

r=me after adding the debug asserts

@compiler-errors
Copy link
Member Author

Added a bunch of debug assertions

@bors r=lcnr

@bors
Copy link
Collaborator

bors commented Jun 26, 2025

📌 Commit 0d37587 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants